package com.demo.servlet.utils;
import com.aspose.words.*;

public class doGenerate {

    //设置段落的标题
    public static void setParagraphTitle(DocumentBuilder builder, String title, int fontSize, int indent) throws Exception
    {
        builder.getFont().setName("黑体");
        builder.getFont().setSize(fontSize);
        builder.getFont().setBold(true); //标题加粗
        builder.getParagraphFormat().setFirstLineIndent(indent); //首字不缩进
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT); //水平居中对齐; //左对齐
        builder.writeln(title);
    }

    //设置页面的标题
    public static void setPageTitle(DocumentBuilder builder, String title, int fontSize) throws Exception
    {
        //文档的标题
        builder.getFont().setName("黑体");
        builder.getFont().setSize(fontSize);
        builder.getFont().setBold(true); //标题加粗
        builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); //水平居中对齐
        builder.writeln(title);
        builder.writeln();
    }

    //拼写正文内容
    private static void setParagraphContext(DocumentBuilder builder, String context) throws Exception
    {
        builder.getFont().setBold(false);;//不加粗
        builder.getParagraphFormat().setFirstLineIndent(24); //缩进
        builder.getFont().setSize(14);;//字体大小
        builder.getFont().setName("华文仿宋");;

        if (context == null)
        {
            builder.writeln("暂无数据");
        }
        else
        {
            builder.writeln(context);
        }

    }

    public static void addChart(DocumentBuilder builder, int chartType, String title, String seriesName, String[] categories, double[] values) throws Exception
    {
        builder.getParagraphFormat().setFirstLineIndent(0); //首字不缩进
        builder.writeln("");



        Shape shape = null;
        switch (chartType){
            case 0: shape = builder.insertChart(ChartType.PIE,132, 152);break;
            case 1: shape = builder.insertChart(ChartType.LINE,432, 252);break;
            case 2: shape = builder.insertChart(ChartType.BAR,432, 252);break;
            case 3: shape = builder.insertChart(ChartType.AREA,432, 252);break;
            default:break;
        }


        shape.setVerticalAlignment(VerticalAlignment.CENTER);
        shape.setHorizontalAlignment(HorizontalAlignment.CENTER);
        shape.setAllowOverlap(false); //不覆盖

        com.aspose.words.Chart chart = shape.getChart();
        ChartSeriesCollection seriesCollection = chart.getSeries();



        seriesCollection.clear();//清除默认
        chart.getLegend().setPosition(LegendPosition.TOP);//显示在顶上

        chart.getTitle().setShow(true);

        // Setting chart Title.
        chart.getTitle().setText(title);

        chart.getTitle().setOverlay(false);//不覆盖
        chart.getLegend().setOverlay(false);



        ChartSeries chartSeries =seriesCollection.add(seriesName, categories, values);;





        //设置数据显示
        //setChartSeriesDataLabel(chatSeries, categories.length);
        //chatSeries.hasDataLabels(true);
        chartSeries.setSmooth(true);

        builder.writeln("");
        builder.getParagraphFormat().setFirstLineIndent(2);

    }

    public static void drawSexChat(DocumentBuilder builder){


        String txt = "2016级新疆维吾尔自治区职业院校实习生采集数据，男生占比50.98%；女生占比49.02%，基本上满足1:1的数据样本";



        String[] label={"男","女"};
        double[] value={581,926};

        try {

            setParagraphContext(builder,txt);
            addChart(builder,1,"男女性别比例","2018",label,value);
        } catch (Exception e){
            System.out.println(e.toString());
        }

    }

    public static void setPages(DocumentBuilder builder) throws Exception{
        //设置纸张布局
        builder.getPageSetup().setPaperSize(PaperSize.LETTER);

        //region 设置word全局的字体样式和字体大小
        builder.getRowFormat().getBorders().setLineStyle(LineStyle.THICK);
        builder.getRowFormat().setHeightRule(HeightRule.AUTO);
        //一行间距1.0等于12点。所以1.5行=18点
        builder.getParagraphFormat().setLineSpacing(18);//设置行间距
    }



    public static void main(String[] args) throws Exception{

        Document doc = new Document();
       DocumentBuilder builder = new DocumentBuilder(doc);


        setPages(builder);
        setPageTitle(builder,"校级实习分析报告",24);


        setParagraphTitle(builder,"一、实习生指标数据分析",20,0);
        setParagraphTitle(builder,"(一) 性别比例",18,2);

        drawSexChat(builder);

        addChart(builder,1,"ss","asdsad",new String[]{"这是哈啊"},new double[]{100});



        // Aspose.Words supports saving any document in many more formats.
        doc.save("HelloWorld_out_.docx");


        //ExEnd:CreateNewDocument
        System.out.println("New Word document created successfully.");

    }
}